package com.andnnl.mongo2;

import com.andnnl.mongo.MongoDAO;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class Mobile2DAO extends Mongo2DAO<Mobile2Entity> {
    private static final Logger logger = LoggerFactory.getLogger(Mobile2DAO.class);

    public static void main(String[] agrs) {


        Mobile2Entity u = new Mobile2Entity();
        u.setMobile(18589025719L);
        u.setType(11);
        Mobile2DAO user = new Mobile2DAO();
        user.insert(u);

        find();
        // UserEntity ue = user.findById("5771f8dde79dfa4c8c5785e9");
        // System.out.println(ue.getName());

		/*
		for (int i = 0; i < 1000; i++)
		{
			UserDAO user = new UserDAO();
			long t1 = System.currentTimeMillis();
			BsonDocument filter = new BsonDocument();
			BasicDBObject sort = new BasicDBObject();
			sort.put("age", 1);
			List<UserEntity> lst = user.getPageList(filter, sort, 1, 20);
			long t2 = System.currentTimeMillis();
			System.out.println("耗时：" + i + "," + (t2 - t1));
			for (UserEntity uu : lst)
			{
				System.out.println(uu.getName());
			}
		}

		try
		{
			Thread.sleep(5000);
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
    }

    public static void find() {
        Mobile2DAO user = new Mobile2DAO();
        Document filter = Document.parse("{type:{$gte:0, $lte:20}}");
        List<Mobile2Entity> lst = user.getPageListOrderById(filter, null, 20);

        for (Mobile2Entity uu : lst) {
            logger.info("" + uu.getMobile());
        }
    }

    public Mobile2Entity findByMobile(long mobile)
    {
        Document filter = new Document("_id", mobile);
        FindIterable<Mobile2Entity> fi=_collection.find(filter).limit(1);
        MongoCursor<Mobile2Entity> cursor=fi.iterator();
        Mobile2Entity t=null;
        if(cursor.hasNext())
        {
             t= cursor.next();
        }
        cursor.close();
        return t;
    }

    public List<Mobile2Entity> findInList(List<Long> mobile)
    {
        List<Mobile2Entity> list=new ArrayList<>();
//        DBObject queryCondition = new BasicDBObject();
//        BasicDBList values = new BasicDBList();
//        for (Long aLong : mobile) {
//            values.add(aLong);
//        }
//        queryCondition.put("mobile", new BasicDBObject("$in", values));
//
//        DBCursor dbCursor = _collection.find(queryCondition);

        Document filter = new Document("_id", new Document("$in",mobile));
//        logger.info(filter.toJson());
        FindIterable<Mobile2Entity> fi=_collection.find(filter);
        MongoCursor<Mobile2Entity> cursor=fi.iterator();
//        MobileEntity t=null;
        while (cursor.hasNext())
        {
            Mobile2Entity t= cursor.next();
            list.add(t);
        }
        cursor.close();
        return list;
    }

    public Mobile2DAO() {
        this.setDbName("sms_base");
        this.setTableName("mobile2");
        this.getCollection(new Mobile2Codec());
    }

    public boolean insert(Mobile2Entity u) {

        this.save(u);

        return false;
    }

    public boolean insert(List<Mobile2Entity> u) {

        this.save(u);

        return false;
    }

    @Override
    protected Class<Mobile2Entity> getEntityClass() {
        return Mobile2Entity.class;
    }

}
